<?php
namespace  Ekadmin\Controller;
use \Think\Controller;
use Think\Page;


class TuansignController extends CommonController

{

        public  function index()

        {
             $where ="uniacid=". $this->uniacid;
             $param = I('get.');
             
             if($param["status"]){
                 $where.=" and status=".$param["status"];
                 
             }
             
             $this->status=$param["status"];

             if($param["mobile"]){
                 $where.=" and mobile=".$param["mobile"];
             }
             
             if($param["name"]){
                 $where.=" and name like '%".$param["name"]."%'";
             }            
            
            $obj = M('beginer_apply');

            $count = $obj->where($where)->count();
            
            $page = new Page($count);
            
            $list = $obj->where($where)->limit($page->firstRow.','.$page->listRows)->order('id desc')->select();
            
            if($list){
                for($i=0;$i<count($list);$i++){
                    $rs=M("user")->find($list[$i]["uid"]);
                    $list[$i]["acv"]=$rs["head"];
                    $list[$i]["zstatus"]=$rs["status"];
//                     $rs=M("xiaoqu")->find($list[$i]["xiaoqu_id"]);
//                     if($rs){
//                         $list[$i]["xiaoquname"]=$rs["name"];
//                     }else{
//                         $list[$i]["xiaoquname"]="无";
//                     }
                }
            }

            $this->assign('page',$page->show());
            
            $this->assign('count',$count);

            $this->assign('list',$list);

            $this->display();

        }



        /**
         *@是否处理[这个操作不用了]
         */
        public function isTuansign(){
            if(IS_POST){
            
                $param = I('id');
            
                $rs=M("beginer_apply")->field('id,status')->find($param);
            
                $map['status']=$rs['status']==1? 2:1;
            
                $map['id'] = $param;
                
                $map["uniacid"]=$this->uniacid;
            
                M('beginer_apply')->save($map);
            
                ajax_return(0,'操作成功');
            
            }
        }

        
        /**
         *@批量同意处理 
         */
        public function isagree(){
            if(IS_POST){
            
                $param = I('id');
            
                if(is_array($param)){
            
                    $map['id'] = array("in",$param);
            
                }else{
            
                    $map['id'] = $param;
            
                }
                
                $map["uniacid"]=$this->uniacid;
            
                M('beginer_apply')->where($map)->setField("status",2);
                
                $rs=M("beginer_apply")->where($map)->select();
                
                if($rs){
                    for($i=0;$i<count($rs);$i++){
                        $rt=M("user")->where("id=".$rs[$i]["uid"])->find();
                        M("user")->where("id=".$rs[$i]["uid"])->setField("is_tuan",2);
                                                
                        $res=M("xiaoqu")->where("name='".$rs[$i]["xiaoquname"]."'")->find();
                        if($res && intval($res["uid"])<=0){
                            $data=array(
                                "mobile"    =>$rs[$i]["mobile"],
                                "uid"       =>$rs[$i]["uid"],
                                "tuan_name" =>$rs[$i]["name"],
                                "tuan_nickname"=>$rt["nickname"]
                            );
                            M("xiaoqu")->where("name='".$rs[$i]["xiaoquname"]."'")->setField($data);
                            
                        }
                        if(C('code3')){
                            $arr=array(
                                "keyword1"=>array('value'=>$rt['nickname']),
                                "keyword2"=>array('value'=>date('Y-m-d',time())),
                                "keyword3"=>array('value'=>$rs[$i]["xiaoquname"]),
                                "keyword4"=>array('value'=>$rs[$i]["name"])
                            );//要发送的数据
                            //$template_id
                            $at = getAccessToken(C('appid'),C('appkey'));
                            send_msg($at,$rs[$i]["form_id"], $rt["utoken"], C('code3'), '',$arr);
                        }
                        
                    }
                }
                
                
               
                ajax_return(0,'操作成功');
            
            }
        }
        
        /**
         *@批量拒绝处理
         */
        public function isnotagree(){
            if(IS_POST){
        
                $param = I('id');
        
                if(is_array($param)){
        
                    $map['id'] = array("in",$param);
        
                }else{
        
                    $map['id'] = $param;
        
                }
                
                $map["uniacid"]=$this->uniacid;
        
                M('beginer_apply')->where($map)->setField("status",3);
                
                $rs=M("beginer_apply")->where($map)->select();
                if($rs){
                    for($i=0;$i<count($rs);$i++){
                        $rt=M("user")->where("id=".$rs[$i]["uid"])->find();
                    
                        $arr=array(
                           "keyword1"=>array('value'=>$rt["nickname"]),
                           "keyword2"=>array('value'=>$rs[$i]["xiaoquname"]),
                           "keyword3"=>array('value'=>'已拒绝'),
                           "keyword4"=>array('value'=>'官方审核')
                        );//要发送的数据
                        
                        M('beginer_apply')->where('id='.$rs[$i]["id"])->delete();
                        
                        send_msg($rs[$i]["form_id"], $rt["utoken"], C("code3"), '',$arr);
                    
                    }                   
                }

        
                ajax_return(0,'操作成功');
        
            }
        } 
        
        
        public function  edit()

        {
            $map["uniacid"]=$this->uniacid;
            
            $map["id"]=I("id");
            
            $data = M('beginer_apply')->where($map)->find();

            $this->assign('data',$data);

            $this->display('form');

        }









        public function  add()

        {



            $this->display('form');

        }



        public function update()

        {

                if(IS_POST){

                    $param = I('post.');


                    $obj = M('beginer_apply');

                     if($param['id']){

                            $obj->save($param);

                    }else{

                        $param['uniacid']=$this->uniacid; 
                        
                        $param['sign_time']  = time();

                          $obj->add($param);

                    }



                    ajax_return(0,'保存成功',U('index'));



                }

        }



        public function del()

        {

            if(IS_POST){

                $param = I('id');

                if(is_array($param)){
                    
                    $map['id'] = array("in",$param);

                }else{

                    $map['id'] = $param;

                }
                
                $map["uniacid"]=$this->uniacid;


                M('beginer_apply')->where($map)->delete();



                ajax_return(0,'删除成功');

            }

        }


    /*
     *@总团长列表 
     */
    public function zongtuanzhang(){
        $where=" uniacid= ".$this->uniacid;
//         $where['uniacid'] = $this->acid;
        
//         $where['is_zongtuanzhang'] = 2;
        
//         $param = I('get.');
         
//         if($param["status"]){
//             $where.=" and status=".$param["status"];
             
//         }
         
//         $this->status=$param["status"];
        
//         if($param["mobile"]){
//             $where.=" and mobile=".$param["mobile"];
//         }
         
//         if($param["name"]){
//             $where.=" and name like '%".$param["name"]."%'";
//         }
        
        $obj = M('zongtuanzhang');
        
        $count = $obj->where($where)->count();
        
        $page = new Page($count);
        
        $list = $obj->where($where)->limit($page->firstRow.','.$page->listRows)->order('id desc')->select();
        
        if($list){
            for($i=0;$i<count($list);$i++){
                $rs=M("user")->find($list[$i]["uid"]);
                $list[$i]["head"]=$rs["head"];
                $list[$i]["nickname"]=$rs["nickname"];
                $list[$i]["name"]=$rs["name"];
                $list[$i]["mobile"]=$rs["mobile"];
            }
        }
        
        $this->assign('page',$page->show());
        
        $this->assign('count',$count);
        
        $this->assign('list',$list);
        
        $this->display();
    }

    
    /**
     *@取消总团长 
     */
    public function canceltuan(){
        if(IS_POST){
        
            $param = I('id');
        
            $map['id'] = $param;
            
            $map["uniacid"]=$this->uniacid;
            
            $rs=M("zongtuanzhang")->where("id=".$param)->find();
            
            M("user")->where("id=".$rs["uid"])->setField("is_zongtuanzhang",1);
        
            M('zongtuanzhang')->where($map)->delete();
        
            ajax_return(0,'删除成功');
        
        }
    }
    
    /**
     *@小区列表 
     */
    public function xiaoqu(){
        if(!isset($_GET['id']) || intval($_GET["id"])<=0){
            ajax_return(1, '参数不正确');
        }
        
        $id=intval($_GET['id']);
        $this->pid=$id;
        $ob=M('zongtuanzhang')->find($id);
        $ob1=M("user")->find($ob["uid"]);
        
        $str=$ob1['id'].'.<font color="red"><strong>当前总团长：'.$ob1['nickname']."</strong></font>";
        $this->assign("str",$str);
        $allow_xiaoqu_ids=explode(',', $ob['uuid']);
        $this->assign("allow_xiaoqu_ids",$allow_xiaoqu_ids);
        
        //读取商品分类
        $this->info=M("quyu")->where("uniacid=".$this->uniacid)->field('id ,name')->order('sort desc')->select();
        
        $quyu_id=isset($_GET["quyu_id"])? intval($_GET["quyu_id"]):0;
        $name=isset($_GET["name"])? trim($_GET["name"]):'';
        
        $where=' status=1 and uniacid='.$this->uniacid;
        if($quyu_id){
            $where.=" and quyu_id=".$quyu_id;
        }
        if($name!=""){
            $where.=" and name like '%".$name."%'";
        }
        
        $obj = M('xiaoqu');
        
        $count = $obj->where($where)->count();
        
        $page = new Page($count);
        
        $list = $obj->field("id,name,status,is_flag")->where($where)->limit($page->firstRow.','.$page->listRows)->order("id desc")->select();
        
        $this->assign('page',$page->show());
        
        $this->assign('count',$count);
        
        $this->assign('list',$list);
        
        
        $this->display('xiaoqu');
    }
    
    /**
     *@添加总团长小区操作
     */
    public function addzongtaunzhangxiaoqu(){
        if(!isset($_POST["id"]) || !isset($_POST["pid"])){
            ajax_return(1,'非法操作');
        }
        $id=I('id');
        $pid=I('pid');
        $rs=M("zongtuanzhang")->field('id,uuid')->find($pid);
        if($rs['uuid']!=""){
            $allow_xiaoqu_ids=$rs['uuid'].",".$id;
        }else{
            $allow_xiaoqu_ids=$id;
        }
        $arr=array(
            'uuid'=>$allow_xiaoqu_ids
        );
        M("zongtuanzhang")->where("id=".$pid)->setField($arr);
        
        M("xiaoqu")->where("id=".$id)->setField("is_flag",2);
    
        ajax_return(0, '添加成功');
    }
    
    /**
     *@取消总团长小区操作
     */
    public function canelzongtaunzhangxiaoqu(){
        if(!isset($_POST["id"]) || !isset($_POST["pid"])){
            ajax_return(1,'非法操作');
        }
        $id=I('id');
        $pid=I('pid');
        $rs=M("zongtuanzhang")->field('id,uuid')->find($pid);
        $allow_xiaoqu_ids=explode(',',$rs['uuid']);
    
        $key = array_search($id, $allow_xiaoqu_ids);
        if ($key !== false)
            array_splice($allow_xiaoqu_ids, $key, 1);
            if(count($allow_xiaoqu_ids)>0){
                $str=implode(',', $allow_xiaoqu_ids);
                $arr=array(
                    'uuid'=>$str
                );
            }else{
                $arr=array(
                    'uuid'=>''
                );
            }
            M("zongtuanzhang")->where("id=".$pid)->setField($arr);
            
            M("xiaoqu")->where("id=".$id)->setField("is_flag",1);
    
            ajax_return(0, '删除成功');
    }
    
}